<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\Exception;
use think\exception\ValidateException;

use service\JsonService as Json;
use service\UploadService;

use app\admin\controller\BaseController;

use app\common\model\User;
use app\common\model\Job;
use app\common\model\JobQa;
use app\common\model\JobCate;
use app\common\model\JobDistribution;
use app\common\model\Project;
use app\common\model\UserSectionLevel;
use app\common\model\UserSection;
use app\common\model\JobQuality;
use app\common\model\ResumeFieldDepot;
use app\common\model\JobRadioRecord;
use app\common\model\JobTotal;
use app\common\model\JobReward;
use app\common\model\JobFile;
use app\common\model\JobHot;

use app\common\validate\JobValidate;
use LDAP\Result;

class ProjectController extends BaseController
{
    /**
     * 获取项目列表
     */
    public function getProjectList()
    {
        $page  = Request()->param('page', 1);
        $isAll = Request()->param('is_all', 0);
        $isDistribution = Request()->param('is_distribution', 0);

        if($isAll == 1){
            $jobWhere = [];
            if($isDistribution){
                $jobIds = JobDistribution::getCol(['staff_id' => $this->user_id], 'job_id');
                if(!$jobIds) return Json::fail('暂无数据');
                $projectIds = Job::getCol([['id', 'in', $jobIds]], 'project_id');
                $jobWhere[] = [['id', 'in', $projectIds]];
            }

            $list = Project::getList($jobWhere, 0, 0, 0, 'id, name');
            return Json::success('ok', $list);
        }

        $field = 'id, user_id, name, resume_field_ids, remarks, create_time';
        $list = Project::getList([], 1, $page, 20, $field, 'create_time DESC');
        if($list === false) return Json::fail('暂无数据');

        if($isAll == 0){
            foreach($list as &$info){
                if($info['resume_field_ids']){
                    $resumeFieldIds = explode(',', $info['resume_field_ids']);
                    $resumeField = ResumeFieldDepot::getCol([['id', 'in', $resumeFieldIds]], 'label');
                    if($resumeField) $info['resume_field'] = implode('、', $resumeField);
                }else{
                    $info['resume_field'] = '';
                }

                $userName = User::getVal(['id' => $info['user_id']], 'name');
                $info['user_name']  = $userName ? $userName : '';
    
                unset($info['user_id']);
            }
        }

        return Json::success('ok', $list, Project::getNum([]));
    }

    /**
     * 项目发布||编辑
     */
    public function setProject()
    {
        $param['id']      = Request()->param('id', 0);
        $param['name']    = Request()->param('name', '');
        $param['remarks'] = Request()->param('remarks', '');
        $param['resume_field_ids'] = Request()->param('resume_field_ids', '');

        if(empty($param['name'])) return Json::fail('名称不能为空');
        if(empty($param['resume_field_ids'])) return Json::fail('字段不能为空');

        if($param['id'] == 0){
            unset($param['id']);
            $param['user_id']  = $this->user_id;
            $flag = Project::add($param);
        }else{
            $flag = Project::edit($param);
        }

        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 项目删除
     */
    public function delProject()
    {
        $projectId = Request()->param('project_id', 0);
        if($projectId == 0) return Json::fail('数据错误');

        $flag = Project::destroy($projectId);
        if(!$flag) return Json::fail();
        return Json::success();
    }




    /**
     * 获取职位列表
     */
    public function getJobList()
    {
        $page  = Request()->param('page', 1);
        $isAll = Request()->param('is_all', 0);
        $sort  = Request()->param('sort', 0);

        $search       = Request()->param('search', '');
        $recruitType  = Request()->param('recruit_type', 0);
        $education    = Request()->param('education', 0);
        $wage         = Request()->param('wage', '');
        $exp          = Request()->param('exp', '');
        $qualityId    = Request()->param('quality_id', 0);
        $companyName  = Request()->param('company_name', '');
        $projectId    = Request()->param('project_id', 0);
        $statusAnswer = Request()->param('status_answer', 0);

        $where = [];

        $userInfo = User::getFind(['id' => $this->user_id]);
        if(UserSectionLevel::getVal(['id' => $userInfo['section_level_id']], 'level') == 1 || $this->user_id == 1){
            $where[] = ['status', 'in', [0, 1]];
        }else{
            $where[] = ['status', '=', 1];
        }

        if(!empty($search)) $where[] = ['name', 'like', '%' . $search . '%'];
        if($recruitType != 0) $where[] = ['recruit_type', '=', $recruitType];
        if($education != 0) $where[] = ['education', '>', $education];
        if($qualityId != 0) $where[] = ['quality_id', '=', $qualityId];
        if($projectId != 0) $where[] = ['project_id', '=', $projectId];

        if($statusAnswer != 0){
            $jobIds = JobQa::getCol([['answer_time', '=', 0]], 'job_id');

            if($jobIds){
                if($statusAnswer == 1){
                    $where[] = ['id', 'in', $jobIds];
                }else if($statusAnswer == 2){
                    $where[] = ['id', 'not in', $jobIds];
                }
            }else{
                if($statusAnswer == 1) return Json::fail('暂无数据');
            }
        }

        if(!empty($companyName)){
            $companyIds = Project::getCol([['name', 'like', '%' . $companyName . '%']], 'id');
            if(!$companyIds) return Json::fail('暂无数据');
            $where[] = ['project_id', 'in', $companyIds];
        }

        if(!empty($wage)){
            list($wageLow, $wageHigh) = explode('-', $wage);

            if($wageLow > 0) $where[]  = ['salary_low', '>=', $wageLow * 1000];
            if($wageHigh > 0) $where[] = ['salary_high', '<=', $wageHigh * 1000];
        }

        if(!empty($exp)){
            list($expLow, $expHigh) = explode('-', $exp);

            if($expLow > 0) $where[]  = ['exp_low', '>=', $expLow];
            if($expHigh > 0) $where[] = ['exp_low', '<=', $expHigh];
        }

        $list = Job::getJobList($where, $page, $isAll, $sort);
        if(!$list) return Json::fail('暂无数据');
        return Json::success('ok', $list, Job::getNum([]));
    }

    /**
     * 更新职位比例
     */
    public function setJobRatio()
    {
        $jobId        = Request()->param('job_id', 0);
        $realityRadio = Request()->param('reality_radio', 0);
        if($jobId == 0) return Json::fail('职位ID不能为空');
        if($realityRadio == 0) return Json::fail('服务费比例不能为空');

        $flag = Job::edit(['id' => $jobId, 'reality_radio' => $realityRadio]);
        if(!$flag) return Json::fail();

        $info = Job::getFind(['id' => $jobId], 'total, reality_radio');
        if($info['reality_radio'] != $realityRadio) return Json::fail();

        JobRadioRecord::addRadio($jobId, $realityRadio);
        return Json::success('ok', ['total' => $info['total'] * $info['reality_radio']]);
    }

    /**
     * 获取我的任务
     */
    public function getMyJobList()
    {
        $page  = Request()->param('page', 1);
        $sort  = Request()->param('sort', 0);

        $search      = Request()->param('search', '');
        $recruitType = Request()->param('recruit_type', 0);
        $education   = Request()->param('education', 0);
        $wage        = Request()->param('wage', '');
        $exp         = Request()->param('exp', '');
        $qualityId   = Request()->param('quality_id', 0);
        $companyName = Request()->param('company_name', '');
        $statusAnswer = Request()->param('status_answer', '');

        $jobIds = JobDistribution::getCol(['staff_id' => $this->user_id], 'job_id');
        if(!$jobIds) return Json::fail('暂无数据');
        $where[] = ['id', 'in', $jobIds];

        $userInfo = User::getFind(['id' => $this->user_id]);
        if(UserSectionLevel::getVal(['id' => $userInfo['section_level_id']], 'level') == 1 || $this->user_id == 1){
            $where[] = ['status', 'in', [0, 1]];
        }else{
            $where[] = ['status', '=', 1];
        }

        if(!empty($search)) $where[] = ['name', 'like', '%' . $search . '%'];
        if($recruitType != 0) $where[] = ['recruit_type', '=', $recruitType];
        if($education != 0) $where[] = ['education', '>', $education];
        if($qualityId != 0) $where[] = ['quality_id', '=', $qualityId];

        if(!empty($companyName)){
            $companyIds = Project::getCol([['name', 'like', '%' . $companyName . '%']], 'id');
            if(!$companyIds) return Json::fail('暂无数据');
            $where[] = ['project_id', 'in', $companyIds];
        }

        if(!empty($wage)){
            list($wageLow, $wageHigh) = explode('-', $wage);

            if($wageLow > 0 && $wageHigh <= 0){
                $where[]  = ['salary_low', '>=', $wageLow * 1000];
            }else if($wageLow <= 0 && $wageHigh > 0){
                $where[] = ['salary_high', '<=', $wageHigh * 1000];
            }else if($wageLow > 0 && $wageHigh > 0){
                $where[] = ['salary_low', 'between', [$wageLow * 1000, $wageHigh * 1000]];
            }
        }

        if(!empty($exp)){
            list($expLow, $expHigh) = explode('-', $exp);

            if($expLow > 0 && $expHigh <= 0){
                $where[]  = ['exp_low', '>=', $expLow];
            }else if($expLow <= 0 && $expHigh > 0){
                $where[] = ['exp_low', '<=', $expHigh];
            }else if($expLow > 0 && $expHigh > 0){
                $where[] = ['exp_low', 'between', [$expLow, $expHigh]];
            }
        }

        if($statusAnswer != 0){
            $jobIds = JobQa::getCol([['answer_time', '=', 0]], 'job_id');

            if($jobIds){
                if($statusAnswer == 1){
                    $where[] = ['id', 'in', $jobIds];
                }else if($statusAnswer == 2){
                    $where[] = ['id', 'not in', $jobIds];
                }
            }else{
                if($statusAnswer == 1) return Json::fail('暂无数据');
            }
        }

        $list = Job::getJobList($where, $page, 0, $sort);
        if(!$list) return Json::fail('暂无数据');

        return Json::success('ok', $list, Job::getNum($where));
    }

    /**
     * 职位参与人列表
     */
    public function getJobPartakeList()
    {
        $jobId = Request()->param('job_id', 0);

        $list = JobDistribution::JobPartakeList($this->user_id, $jobId);
        if(!$list) return Json::fail('暂无数据');
        return Json::success('ok', $list);
    }

    /**
     * 职位性质列表
     */
    public function getJobQualityList()
    {
        return Json::success('ok', JobQuality::getList([], 0));
    }

    /**
     * 职位发布
     */
    public function addJob()
    {
        $param = input('');

        try{
            validate(JobValidate::class)->scene('add')->check($param);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $userInfo = User::getFind(['id' => $this->user_id]);
        if(UserSectionLevel::getVal(['id' => $userInfo['section_level_id']], 'level') == 1 || $this->user_id == 1) $param['status'] = 1;

        $param['user_id']       = $this->user_id;
        $param['cate_id']       = Job::getCate($param['total']);

        if(isset($param['warranty']) && isset($param['warranty2']) && $param['warranty'] > $param['warranty2']) return Json::fail('保证期不能大于保证期2');

        $flag = Job::add($param);
        if(!$flag) return Json::fail('发布失败');

        JobRadioRecord::addRadio($flag['id'], 0.8);
        return Json::success('发布成功');
    }

    /**
     * 职位编辑
     */
    public function setJob()
    {
        $param = input('');

        try{
            validate(JobValidate::class)->scene('edit')->check($param);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $param['cate_id'] = Job::getCate($param['total']);

        if(isset($param['create_time'])) unset($param['create_time']);

        $flag = Job::edit($param);
        if(!$flag) return Json::fail('编辑失败');
        return Json::success('编辑成功');
    }

    /**
     * 职位通过||驳回
     */
    public function setJobStatus()
    {
        $JobId  = Request()->param('job_id', 0);
        $status = Request()->param('status', 0);

        if($status != 1 && $status != 2) return Json::fail('参数错误');

        $flag = Job::edit(['id' => $JobId, 'status' => $status]);
        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 职位删除
     */
    public function delJob()
    {
        $jobId = Request()->param('job_id', 0);
        if($jobId == 0) return Json::fail('数据错误');

        $flag = Job::destroy($jobId);
        if(!$flag) return Json::fail('删除失败');
        return Json::success('删除成功');
    }

    /**
     * 职位上架||下架
     */
    public function setJobIsShow()
    {
        $jobId = Request()->param('job_id', 0);
        if($jobId == 0) return Json::fail('数据错误');

        $isShows = Job::getVal(['id' => $jobId], 'is_show');
        if($isShows == 1){
            $isShow = 0;
            $msg    = '下架';
        }else{
            $isShow = 1;
            $msg    = '上架';
        }

        $flag = Job::edit(['id' => $jobId, 'is_show' => $isShow]);
        if(!$flag) return Json::fail($msg . '失败');
        return Json::success($msg . '成功');
    }

    /**
     * 职位分配
     */
    public function setDis()
    {
        $jobId    = Request()->param('job_id', 0);
        $staffIds = Request()->param('user_ids', '');
        if($jobId == 0) return Json::fail('职位不能为空');
        if(empty($staffIds)) return Json::fail('参与人不能为空');

        $flag = JobDistribution::addJobDistribution($this->user_id, $staffIds, $jobId);
        
        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 职位取消分配
     */
    public function delDis()
    {
        $jobDisId    = Request()->param('job_dis_id', 0);
        if($jobDisId == 0) return Json::fail('分配ID不能为空');

        $flag = JobDistribution::delJobDistribution($jobDisId);
        
        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 职位未分配员工列表
     */
    public function getJobUserList()
    {
        $jobId = Request()->param('job_id', 0);
        $list = JobDistribution::notJobUserList($this->user_id, $jobId);
        if(!$list) return Json::fail('暂无数据');
        return Json::success('ok', $list);
    }



    /**
     * 获取职位问答列表
     */
    public function getJobQaList()
    {
        $page  = Request()->param('page', 1);
        $jobId = Request()->param('job_id', 0);

        $where[] = ['job_id', '=', $jobId];

        $list = JobQa::getList($where, 1, $page, 20, '*', 'create_time DESC');
        if($list === false) return Json::fail('暂无数据');

        foreach($list as &$info){
            $info['job_name']      = Job::getVal(['id' => $info['job_id']], 'name');
            $info['question_name'] = User::getVal(['id' => $info['admin_id']], 'name');
            $info['answer_name']   = User::getVal(['id' => $info['user_id']], 'name');
            $info['answer_time']   = $info['answer_time'] ? date('Y-m-d H:i:s', $info['answer_time']) : '';

            unset($info['job_id']);
            unset($info['user_id']);
            unset($info['admin_id']);
        }

        return Json::success('ok', $list, JobQa::getNum($where));
    }

    /**
     * 问题发布
     */
    public function setJobQa()
    {
        $id      = Request()->param('id', 0);
        $jobId   = Request()->param('job_id', 0);
        $content = Request()->param('content', '');

        $param['user_id']  = $this->user_id;

        if($id){
            $flag = JobQa::edit(['id' => $id, 'admin_id' => $this->user_id, 'answer' => $content, 'answer_time' => time()]);
        }else{
            $flag = JobQa::add(['job_id' => $jobId, 'user_id' => $this->user_id, 'question' => $content]);
        }

        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 问答删除
     */
    public function delJobQa()
    {
        $jobQaId = Request()->param('job_qa_id', 0);
        if($jobQaId == 0) return Json::fail('数据错误');

        $flag = JobQa::destroy($jobQaId);
        if(!$flag) return Json::fail('删除失败');
        return Json::success('删除成功');
    }



    /**
     * 获取职位类别列表
     */
    public function getJobCateList()
    {
        $list = JobCate::getList([], 0, 0, 0, '*', 'total DESC');
        if($list === false) return Json::fail('暂无数据');
        return Json::success('ok', $list, JobCate::getNum([]));
    }

    /**
     * 职位类别新增||编辑
     */
    public function setJobCate()
    {
        $param['id']    = Request()->param('id', 0);
        $param['name']  = Request()->param('name', '');
        $param['total'] = Request()->param('total', '');

        if(empty($param['name'])) return Json::fail('名称不能为空');
        if(empty($param['total'])) return Json::fail('服务费不能为空');

        if($param['id'] == 0){
            unset($param['id']);
            $flag = JobCate::add($param);
        }else{
            $flag = JobCate::edit($param);
        }
        
        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 职位类别删除
     */
    public function delJobCate()
    {
        $cateId = Request()->param('cate_id', 0);
        if($cateId == 0) return Json::fail('数据错误');

        if(Job::getFind(['cate_id' => $cateId]) != false) return Json::fail('该类别正在使用中，不能删除');

        $flag = JobCate::destroy($cateId);
        if(!$flag) return Json::fail('删除失败');
        return Json::success('删除成功');
    }



    /**
     * 职位服务费新增和编辑
     */
    public function setJobTotal()
    {
        $jobTotalId = Request()->param('job_total_id', 0);
        $jobId      = Request()->param('job_id', 0);
        $joinNum    = Request()->param('join_num', 0);
        $total      = Request()->param('total', 0);

        if($jobId == 0) return Json::fail('职位ID不能为空');
        if($joinNum == 0) return Json::fail('招聘人数不能为空');
        if($total == 0) return Json::fail('服务费不能为空');

        $data = [
            'job_id'     => $jobId,
            'join_num'   => $joinNum,
            'total'      => $total,
        ];

        if($jobTotalId){
            $data['id'] = $jobTotalId;
            $flag = JobTotal::edit($data);
        }else{
            $flag = JobTotal::add($data);
        }

        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 职位服务费列表
     */
    public function getJobTotalList()
    {
        $jobId = Request()->param('job_id', 0);
        $list = JobTotal::getList(['job_id' => $jobId], 0);
        if(!$list) return Json::fail('暂无数据');
        return Json::success('ok', $list);
    }

    /**
     * 职位服务费删除
     */
    public function delJobTotal()
    {
        $jobTotalId = Request()->param('job_total_id', 0);

        $flag = JobTotal::destroy($jobTotalId);
        if(!$flag) return Json::fail();
        return Json::success();
    }



    /**
     * 职位奖励新增和编辑
     */
    public function setJobReward()
    {
        $jobRewardId = Request()->param('job_reward_id', 0);
        $achieLow    = Request()->param('achie_low', 0);
        $achieHigh   = Request()->param('achie_high', 0);
        $rewardRadio = Request()->param('reward_radio', 0);
        $rewardOther = Request()->param('reward_other', 0);
        $desc        = Request()->param('desc', '');

        if($achieLow == 0) return Json::fail('业绩（低）不能为空');
        if($achieHigh == 0) return Json::fail('业绩（高）不能为空');
        if($rewardRadio == 0) return Json::fail('奖励参数不能为空');

        $data = [
            'achie_low'    => $achieLow,
            'achie_high'   => $achieHigh,
            'reward_radio' => $rewardRadio,
            'reward_other' => $rewardOther,
            'desc'         => $desc,
        ];

        if($jobRewardId){
            $data['id'] = $jobRewardId;
            $flag = JobReward::edit($data);
        }else{
            $flag = JobReward::add($data);
        }

        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 职位奖励列表
     */
    public function getJobRewardList()
    {
        $list = JobReward::getList([], 0);
        if(!$list) return Json::fail('暂无数据');
        return Json::success('ok', $list);
    }

    /**
     * 职位奖励删除
     */
    public function delJobReward()
    {
        $jobRewardId = Request()->param('job_reward_id', 0);

        $flag = JobReward::destroy($jobRewardId);
        if(!$flag) return Json::fail();
        return Json::success();
    }



    /**
     * 职位悬赏新增和编辑
     */
    public function setJobHot()
    {
        $jobHotId  = Request()->param('job_hot_id', 0);
        $jobId     = Request()->param('job_id', 0);
        $price     = Request()->param('price', 0);
        $joinNum   = Request()->param('join_num', 0);
        $startTime = Request()->param('start_time', 0);
        $endTime   = Request()->param('end_time', 0);

        if($jobId == 0) return Json::fail('职位ID不能为空');
        if($price == 0) return Json::fail('悬赏价格不能为空');
        if($joinNum == 0) return Json::fail('招聘人数不能为空');
        if($startTime == 0) return Json::fail('开始时间不能为空');
        if($endTime == 0) return Json::fail('结束时间不能为空');

        if(strtotime($endTime) <= time()) return Json::fail('结束时间必须大于现在的时间');
        if(!$jobHotId && JobHot::getVal([['job_id', '=', $jobId], ['end_time', '>=', time()]], 'id')) return Json::fail('还有未结束的悬赏，不能新增');

        $data = [
            'job_id'     => $jobId,
            'user_id'    => $this->user_id,
            'price'      => $price,
            'join_num'   => $joinNum,
            'start_time' => strtotime($startTime),
            'end_time'   => strtotime($endTime),
        ];

        if($jobHotId){
            $data['id'] = $jobHotId;
            $flag = JobHot::edit($data);
        }else{
            $flag = JobHot::add($data);
        }

        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 职位悬赏列表
     */
    public function getJobHotList()
    {
        $jobId = Request()->param('job_id', 0);
        $page  = Request()->param('page', 1);
        $isAll = Request()->param('is_all', 0);

        $limit = 20;
        if($isAll) $limit = 100;

        $where = [];

        if(!$jobId) $where[] = ['end_time', '>=', time()];
        if($jobId) $where[] = ['job_id', '=', $jobId];

        $list = JobHot::getList($where, 1, $page, $limit, '*', 'end_time DESC');
        if(!$list) return Json::fail('暂无数据');

        foreach($list as &$info){
            $jobInfo = Job::getFind(['id' => $info['job_id']], 'name, total, reality_radio');

            $info['job_name']   = $jobInfo['name'];
            $info['job_total']  = $jobInfo['total'] * $jobInfo['reality_radio'];
            $info['start_time'] = date('Y-m-d H:i', $info['start_time']);
            $info['end_time']   = date('Y-m-d H:i', $info['end_time']);
        }

        return Json::success('ok', $list);
    }

    /**
     * 职位悬赏删除
     */
    public function delJobHot()
    {
        $jobHotId = Request()->param('job_hot_id', 0);

        $flag = JobHot::destroy($jobHotId);
        if(!$flag) return Json::fail();
        return Json::success();
    }



    /**
     * 职位附件列表
     */
    public function getJobFileList()
    {
        $page  = Request()->param('page', 1);
        $jobId = Request()->param('job_id', 0);

        $list = JobFile::getList(['job_id' => $jobId], 1, $page, 20, 'id, file_name, file_size, file_url, file_mime, storage_location', 'id DESC');
        if(!$list) return Json::fail('暂无数据');

        foreach($list as &$info){
            if($info['storage_location'] == 1) $info['file_url']  = getUrl() . $info['file_url'];
            $info['file_size'] = fileSizeTrans($info['file_size']);
        }

        return Json::success('ok', $list, JobFile::getNum(['job_id' => $jobId]));
    }

    /**
     * 职位附件上传
     */
    public function setJobFile()
    {
        $jobId = Request()->param('job_id', 0);
        $file  = Request()->file('file', '');

        $format = ['xlsx', 'xls', 'doc', 'docx', 'jpg', 'png', 'jpeg', 'pdf', 'mp3', 'wav', 'ogg', 'mp4', 'webm'];

        if(empty($file)) return Json::fail('文件不能为空');
        if(!in_array($file->extension(), $format)) return Json::fail('文件上传类型错误');
        
        if(!Job::getVal(['id' => $jobId], 'name')) return Json::fail('职位不存在');

        /** 上传文件（保留原文件名） */
        // $fileUpPath = \think\facade\Filesystem::disk('public')->putFile('job_file', $file,
        //     function () use ($file){
        //         return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
        //     }
        // );

        // $filePath = str_replace('\\','/', $fileUpPath);

        $filePath = UploadService::upload($file);

        $flag = JobFile::add([
            'job_id'           => $jobId,
            'file_name'        => $file->getOriginalName(),  /** 原始文件名 */
            'file_size'        => $file->getSize(),  /** 文件大小，单位：B */
            // 'file_url'         => '/storage/' . $filePath,  /** 文件上传地址 */
            'file_url'         => $filePath,  /** 文件上传地址 */
            'file_mime'        => explode('/', $file->getMime())[0],
            'storage_location' => 2,  /** 存储位置 */
        ]);

        if(!$flag) return Json::fail();
        return Json::success();
    }

    /**
     * 职位附件下载
     */
    public function downJobFile()
    {
        $jobFileId = Request()->param('job_file_id', 0);

        $jobFileInfo = JobFile::getFind(['id' => $jobFileId]);
        if(!$jobFileInfo) return Json::fail('文件不存在');

        if($jobFileInfo['storage_location'] == 1){
            return download(\think\facade\App::getRootPath() . '/public/' . $jobFileInfo['file_url'], $jobFileInfo['file_name'], true);
        }else{
            return download($jobFileInfo['file_url'], $jobFileInfo['file_name'], true);
        }
        
    }

    /**
     * 职位附件删除
     */
    public function delJobFile()
    {
        $jobFileId = Request()->param('job_file_id', 0);

        $jobFileInfo = JobFile::getFind(['id' => $jobFileId]);
        if(!$jobFileInfo) return Json::fail('文件不存在');

        $flag = JobFile::destroy($jobFileId);
        if(!$flag) return Json::fail();

        @unlink(\think\facade\App::getRootPath() . '/public/' . $jobFileInfo['file_url']);
        return Json::success();
    }
}